package no63;

class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int length = obstacleGrid.length;
        int column = obstacleGrid[0].length;
        int[][] down = new int[length][column-1];
        int[][] right = new int[length-1][column];
        if (length == 1 && column == 1){
            return 1;
        }else if (length == 1){
            for (int i = 0; i < obstacleGrid[0].length; i++) {
                if (obstacleGrid[0][i] == 1)
                    return 0;
            }
            return 1;
        }else if (column == 1){
            for (int i = 0; i < obstacleGrid.length; i++) {
                if (obstacleGrid[i][0] == 1)
                    return 0;
            }
            return 1;
        }

        if (obstacleGrid[0][1] == 1 && obstacleGrid[1][0] == 1)  {
            return 0;
        } else if (obstacleGrid[0][1] == 0 && obstacleGrid[1][0] == 0){
            for (int i = 0; i < length ; i++) {
                for (int j = 0; j < column -1; j++) {
                    down[i][j] = obstacleGrid[length][column+1];
                }
            }
            for (int i = 0; i < length -1 ; i++) {
                for (int j = 0; j < column ; j++) {
                    down[i][j] = obstacleGrid[length + 1][column];
                }
            }
            return Math.max(uniquePathsWithObstacles(down), uniquePathsWithObstacles(right));
        } else if (obstacleGrid[0][1] == 1){
            return uniquePathsWithObstacles(down);
        } else {
            return uniquePathsWithObstacles(right);
        }





    }
}